<?php
spl_autoload_register('Bootstrap::autoLoad');
abstract class Bootstrap
{
    public static $uri;
    static function loadConfig()
    {
        $config = [];
    }
    static function run()
    {
        if(DEBUG) {
            error_reporting(E_ALL);
        } else error_reporting(0);
        $sApiName = php_sapi_name();
        static::loadConfig();
        if ($sApiName != 'cli') {
        }
        static::router();
    }
    static function router()
    {
        //$path = $_SERVER['PATH_INFO'];
        $path = $_SERVER['REQUEST_URI'];
        $URI = parse_url($path);
        $path = $URI['path'];
        if(empty($path) || $path == '/') {
            $path = 'index/index';
        }

        $path = ltrim($path,'/');

        $router = array_filter(explode('/', $path));

        $cName = ucfirst($router[0]) . 'Controller';
        $aName = isset($router[1]) ? lcfirst($router[1]) . 'Action' : 'indexAction';

        $controller = new $cName(ucfirst($router[0]), strtolower($router[1]));
        if(is_callable(array($controller, 'behaviors'))) {
            $controller->behaviors();
        }

        $controller->$aName();
    }

    public static function autoLoad($class)
    {
        if(strpos($class, 'Controller') > -1) {
            $class = str_replace('Controller', '', $class);
            require APPLICATION_PATH.'/application/controllers/' . $class .'.php';
        }
    }
}